# coding: utf-8

from __future__ import (print_function, unicode_literals)

import sys
import json

sys.path = ['../..'] + sys.path
from query_rewrite import util


pinyin2hanzi_file = '../hmm/result/pinyin2hanzi.txt'
base_emission_file = '../hmm/result/base_emission.json'

output_file = 'result/dag_char.json'


def writejson2file(obj, filename):
    with open(filename, 'w', encoding='UTF-8') as outfile:
        data = json.dumps(obj, indent=4, sort_keys=True)
        outfile.write(data)

def readdatafromfile(filename):
    with open(filename, encoding='UTF-8') as outfile:
        return json.load(outfile)


def generate_char():
    # 每个拼音对应的汉字及概率
    result = {}
    # 每个汉字对应的拼音及其概率
    data = readdatafromfile(base_emission_file)

    max_num = 0.
    min_num = 100000000000000.

    # 从base_emission写入result
    for hanzi in data:
        for pinyin in data[hanzi]:
            pinyin = util.simplify_pinyin(pinyin)
            num = data[hanzi][pinyin]
            key = pinyin
            result.setdefault(key, {})
            result[key].setdefault(hanzi, 0)
            result[key][hanzi] += num
            max_num = max(max_num, result[key][hanzi])
            min_num = min(min_num, result[key][hanzi])

    # 从pin2hanzi.txt写入result
    for line in open(pinyin2hanzi_file, encoding='UTF-8'):
        line = util.as_text(line.strip())
        if '=' not in line:
            continue
        pinyin, chars = line.split('=')
        if len(pinyin) == 0 or len(chars) == 0:
            continue

        pinyin = util.simplify_pinyin(pinyin)

        for hanzi in chars:
            key = pinyin
            result.setdefault(key, {})
            result[key].setdefault(hanzi, 0)
            result[key][hanzi] += 1.
            max_num = max(max_num, result[key][hanzi])
            min_num = min(min_num, result[key][hanzi])

    result['max_num'] = max_num
    result['min_num'] = min_num
    # 将result存储到dag.json文件
    writejson2file(result, output_file)